package com.tools.common.object.string;

import com.tools.common.object.Note;

import java.util.Objects;

/**
 * 长度验证方法
 * */
@Note("长度验证方法")
public class ValidLength implements OwnValid {

    @Note("最小长度。负数则不验证最小值")
    private final int min;

    @Note("最大长度。负数则不验证最大值")
    private final int max;

    /* ************************************************************************************************************
     *
     *      构造器
     *
     * ************************************************************************************************************
     * */

    public ValidLength() {
        this(-1, -1);
    }

    public ValidLength(int max) {
        if(max < 0) throw new IllegalArgumentException("指定的长度为负数");
        this.max = max;
        this.min = -1;
    }

    public ValidLength(int min, int max) {
        this.min = min;
        this.max = max;
    }

    public static ValidLength of() {
        return new ValidLength();
    }

    public static ValidLength of(int max) {
        return new ValidLength(max);
    }

    public static ValidLength of(int min, int max) {
        return new ValidLength(min, max);
    }


    /* ************************************************************************************************************
     *
     *      Getter / Setter
     *
     * ************************************************************************************************************
     * */

    public int getMin() {
        return min;
    }

    public int getMax() {
        return max;
    }

    @Override
    public String toString() {
        return "ValidLength{" +
                "min=" + min +
                ", max=" + max +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ValidLength that = (ValidLength) o;
        return min == that.min && max == that.max;
    }

    @Override
    public int hashCode() {
        return Objects.hash(min, max);
    }


    /* ************************************************************************************************************
     *
     *      功能方法
     *
     * ************************************************************************************************************
     * */

    @Note("length 必须在 (min, max) 之间")
    public boolean in(int length) {
        return (length > min) && (length < max);
    }

    @Note("min 必须大于 0 且 length 必须在 (min, max] 之间")
    public boolean erIn(int length) {
        return (length > min) && (length <= max);
    }

    @Note("min 必须大于 0 且 length 必须在 [min, max) 之间")
    public boolean elIn(int length) {
        return (length >= min) && (length < max);
    }

    @Note("min 必须大于 0 且 length 必须在 [min, max] 之间")
    public boolean elrIn(int length) {
        return (length >= min) && (length <= max);
    }


    public boolean eqMax(int length) {
        return length == max;
    }

    public boolean lteMax(int length) {
        return length <= max;
    }

    public boolean gteMax(int length) {
        return length >= max;
    }

    public boolean ltMax(int length) {
        return length < max;
    }

    public boolean gtMax(int length) {
        return length > max;
    }


    public boolean eqMin(int length) {
        return length == min;
    }

    public boolean lteMin(int length) {
        return length <= min;
    }

    public boolean gteMin(int length) {
        return length >= min;
    }

    public boolean ltMin(int length) {
        return length < min;
    }

    public boolean gtMin(int length) {
        return length > min;
    }
}
